app.service('todayService',function($http, $q){
	var menus = [];
	var def =  $q.defer();
	$http.get('./data/menu.json').success(function(data){
		for(var i = 0,len=data.menu_list[0].subtab.length;i < len;i++){
			menus.push(data.menu_list[0].subtab[i].title);
		}
		def.resolve(menus);
	});
	return def.promise;
})
.service('todayServiceAdvert', function($http, $q){
	var goodsInfo = {},
	//存储轮播图图片
	banner = [],
	//存储公告信息
	advert = [],
	//模块数据信息
	module_productType = [],
	//活动主题数据
	titleH = "",
	//存储商品数据
	todayGoodsInfo = [],
	//存储底部菜单数据
	main_type = [];
	var def =  $q.defer();
	$http.get('./data/product.json').success(function(data){
		
		//轮播图片的src
		for(var i = 0,len = data.data.banner_ads.length;i < len;i++){
			//商品分类列表
			banner.push(data.data.banner_ads[i].pic);
		}
		//获取促销活动数据
		for(var i = 0,len = data.data.module_ads.multi_block[0].data[0].child.length;i < len;i++){
			var advert_info = {};
			//广告图片
			advert_info.pic_url = data.data.module_ads.multi_block[0].data[0].child[i].pic;
			//活动名称
			advert_info.name = data.data.module_ads.multi_block[0].data[0].child[i].words;
			advert.push(advert_info);
		}
		//获取模块新数据信息
		for(var i = 0,len = data.data.module_ads.multi_block[1].data[0].child.length;i < len;i++){
			//模块图片信息
			module_productType.push( data.data.module_ads.multi_block[1].data[0].child[i].pic);
		}
		//底部菜单数据
		for(var i = 0,len =  data.data.menu_link.menu_list.length;i < len;i++ ){
			var main_theme = {};
			//底部菜单的标题
			main_theme.title = data.data.menu_link.menu_list[i].title;
			//底部菜单图标
			main_theme.act_icon = data.data.menu_link.menu_list[i].act_icon;
			main_theme.bg_icon = data.data.menu_link.menu_list[i].bg_icon;
			main_type.push(main_theme);
		}
		//活动主题数据
		titleH =  data.data.module_ads.multi_block[2].data[0].child[0].pic;
		goodsInfo.banner = banner;
		goodsInfo.advert = advert;
		goodsInfo.module_productType = module_productType;
		goodsInfo.titleH = titleH;
		goodsInfo.main_type = main_type;
			def.resolve(goodsInfo);
	});

	return def.promise
})
.factory('Demo', function ($http) {
     var Demo = function () {
         this.items = [];
        this.busy = false;
         this.after = '';
         this.page = 6;

     };     
     Demo.prototype.nextPage = function () {
         if (this.busy) return;
         this.busy = true;         
        var url = 'https://shop.juanpi.com/gsort?key=zuixinzhekou&type=1&zhouyi_ids=p8_c3_a1_l1_18_51_5&machining=hotcoupon&page='+ this.page +'&rows=10&dtype=JSONP&cm=1&cm_channel=1&callback=JSON_CALLBACK';
        $http.jsonp(url).success(function (data) {
             var items = data;
             for (var i = 0; i < items.list.length; i++) {
           			var todayGoodInfo = {};
				 	if(data.list[i].coupon){
			 				//商品类型
				 			todayGoodInfo.residue =  data.list[i].residue
				 		//商品打折信息
				 			todayGoodInfo.bankInfo = data.list[i].coupon.rules[0].aeBankInfo;
				 		//商品名称
				 			todayGoodInfo.name = data.list[i].coupon.abName;
				 		//商品图片地址
				 			todayGoodInfo.pic_url = data.list[i].pic_url;
			 			
				 	}else{
				 		todayGoodInfo.residue =  data.list[i].residue;
				 		todayGoodInfo.bankInfo =  data.list[i].main_title;
				 		todayGoodInfo.name =  data.list[i].sub_title
				 		todayGoodInfo.imgUrl  =  data.list[i].pic_url;
				 	}
                 this.items.push(todayGoodInfo);
			 			             }
            // this.after = "t3_" + this.items[this.items.length - 1].id;
             this.busy = false;
             this.page += 1;
         }.bind(this));
     }; 
     return Demo;
 })
.factory('Advert', function ($http) {
     var Advert = function () {
         this.items = [];
        this.busy = false;
         this.after = '';
         this.page = 1;

     };     
     Advert.prototype.nextPage = function () {
         if (this.busy) return;
         this.busy = true;         
        var url = 'https://shop.juanpi.com/gsort?key=99you&type=1&zhouyi_ids=p8_c3_l4_18_51_5&machining=hotcoupon&page=' + this.page + ' &rows=10&dtype=JSONP&cm=1&cm_channel=8&callback=JSON_CALLBACK';
        $http.jsonp(url).success(function (data) {
            var items = data;
             for (var i = 0; i < items.list.length; i++) {
           			var advertGoodInfo = {};
				 		advertGoodInfo.residue =  data.list[i].residue;
				 		advertGoodInfo.cprice = data.list[i].cprice;
				 		advertGoodInfo.oprice = data.list[i].oprice;
				 		advertGoodInfo.title = data.list[i].title;
				 		advertGoodInfo.pic_url  =  data.list[i].pic_url;
                 this.items.push(advertGoodInfo);
             }
            // this.after = "t3_" + this.items[this.items.length - 1].id;
             this.busy = false;
             this.page += 1;
         }.bind(this));
     }; 
     return Advert;
 });
		
		
